package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.entity.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.LedSecT2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.LedT2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.LedThreeT2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.RegForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.baseRelevanted.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.*;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.baseRelevanted.CombineApplyNotverifyVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.baseRelevanted.CombineApplyQryBackVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.baseRelevanted.CombineApplyVerifyVo;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.status.CloseStatus;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.LeaderJobStateType;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.UserStatus;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.UserType;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.PublicManageBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.*;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.*;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.UserVO;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.vo.ViewpointPublicManageQryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.vo.ViewpointQueryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.viewpoint.vo.ViewpointRecomQueryForm;
import com.cloudbroker.bcs.platform.ltb.middle.entity.*;
import com.cloudbroker.bcs.platform.ltb.middle.entityview.ViewpointUserView;
import com.cloudbroker.bcs.platform.ltb.middle.extservice.lightinvesting.LightinvestingService;
import com.cloudbroker.bcs.platform.ltb.middle.service.*;
import com.cloudbroker.bcs.platform.ltb.middle.util.HttpToBGFormUtil;
import com.cloudbroker.bcs.platform.ltb.middle.util.MD5Util;
import com.cloudbroker.bcs.platform.ltb.middle.util.UserTokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by world on 16/12/6.
 */
@Service
public class PublicManageServiceImpl implements PublicManageBizService {
    @Resource
    private LedT2Service ledT2Service;

    @Resource
    private LedSecT2Service ledSecT2Service;

    @Resource
    private LedThreeT2Service ledThreeT2Service;

    @Resource
    private ViewpointMsgService viewpointMsgService;

    @Resource
    private ViewpointRecomService viewpointRecomService;

    @Resource
    private UserService userService;

    @Resource
    private CombineService combineService;

    @Resource
    private LeaderService leaderService;

    @Resource
    private LightinvestingService lightinvestingService;


    public UserCollectHistoryList userCollectHistoryListQry(PublicManageUserCollectQryForm form) {
        UserCollectHistoryList result = new UserCollectHistoryList();
        FocusCombineForm focusCombineForm = new FocusCombineForm();
        focusCombineForm.setRequest_num(form.getRequest_num());
        focusCombineForm.setPosition_str(form.getPosition_str());
        focusCombineForm.setUser_id(form.getUser_id() == null ? null : String.valueOf(form.getUser_id()));
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
        focusCombineForm.setUser_token(user_token);
        FocusCombineQryBackVo focusCombineQryBackVo = ledT2Service.func_combine_collect_info_qry(focusCombineForm);
        UserTokenUtil.deleteUserToken(user_token);
        List<UserCollectListData> listDataList = new ArrayList<>();
        if (focusCombineQryBackVo.getData_list() != null) {
            for (FocusCombineResult focusCombineResult : focusCombineQryBackVo.getData_list()) {
                UserCollectListData userCollectListData = new UserCollectListData();
                BeanUtils.copyProperties(focusCombineResult, userCollectListData);
                listDataList.add(userCollectListData);
            }
        }
        result.setData_list(listDataList);
        return VOUtil.setSuccessResult(result);
    }

    public UserSignHistoryList userSignHistoryListQry(PublicManageUserSignQryForm form) {
        UserSignHistoryList userSignHistoryList = new UserSignHistoryList();
        ComuserSignQryForm comuserSignQryForm = new ComuserSignQryForm();
        comuserSignQryForm.setPosition_str(form.getPosition_str());
        comuserSignQryForm.setRequest_num(form.getRequest_num());
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
        comuserSignQryForm.setUser_token(user_token);
        comuserSignQryForm.setUser_id(form.getUser_id() == null ? null : String.valueOf(form.getUser_id()));
        ComuserSignQryBackVo comuserSignQryBackVo = ledT2Service.func_comuser_sign_qry(comuserSignQryForm);
        UserTokenUtil.deleteUserToken(user_token);
        List<UserSignListData> userSignListDatas = new ArrayList<>();
        if (comuserSignQryBackVo.getData_list() != null) {
            for (ComuserSignQry comuserSignQry : comuserSignQryBackVo.getData_list()) {
                UserSignListData userSignListData = new UserSignListData();
                BeanUtils.copyProperties(comuserSignQry, userSignListData);
                userSignListDatas.add(userSignListData);
            }
        }
        userSignHistoryList.setData_list(userSignListDatas);
        return VOUtil.setSuccessResult(userSignHistoryList);
    }

    public HSBCPSBackVO publicManageAdviserRecommend(PMAdviserRecommendForm form) {
        if ("1".equals(form.getRecommend_flag())) {
            String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
            RecommendInterestForm recommendInterestForm = new RecommendInterestForm();
            recommendInterestForm.setUser_token(user_token);
            RecommendInterestQryBackVo backVo = ledT2Service.func_recommend_interest_query(recommendInterestForm);
            if (!VOUtil.isSuccess(backVo)) {
                UserTokenUtil.deleteUserToken(user_token);
                return VOUtil.genBGErrorResult(backVo.getError_no(), backVo.getError_info(), HSBCPSBackVO.class);
            }
            if (VOUtil.isSuccess(backVo)&&backVo.getData_list()!=null&&backVo.getData_list().size()>=3) {
                UserTokenUtil.deleteUserToken(user_token);
                return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.BEYOND_RECOMMEND_COUNT, HSBCPSBackVO.class);
            }
            ExtcomacctAdviserRecommendForm form1 = new ExtcomacctAdviserRecommendForm();
            form1.setCompany_id(form.getCompany_id());
            form1.setUser_token(user_token);
            form1.setUser_id(form.getAdviser_id());
            ExtcomacctAdviserRecommendVo back = ledSecT2Service.func_extcomacct_adviser_recommend(form1);
            UserTokenUtil.deleteUserToken(user_token);
            if (!VOUtil.isSuccess(back)) {
                return VOUtil.genBGErrorResult(back.getError_no(), back.getError_info(), HSBCPSBackVO.class);
            }
        } else if ("2".equals(form.getRecommend_flag())) {
            String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
            ExtcomacctAdviserRecommendCancelForm form1 = new ExtcomacctAdviserRecommendCancelForm();
            form1.setCompany_id(form.getCompany_id());
            form1.setUser_token(user_token);
            form1.setUser_id(form.getAdviser_id());
            ExtcomacctAdviserRecommendCancelVo back = ledSecT2Service.func_extcomacct_adviser_recommend_cancel(form1);
            UserTokenUtil.deleteUserToken(user_token);
            if (!VOUtil.isSuccess(back)) {
                return VOUtil.genBGErrorResult(back.getError_no(), back.getError_info(), HSBCPSBackVO.class);
            }
        } else {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.RECOMMEND_FLAG_NOT_EXIST
                    , HSBCPSBackVO.class);
        }
        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }

    public HSBCPSBackVO publicManageViewpointRecommend(PMViewpointRecommendForm form) {
        if ("1".equals(form.getRecommend_flag())) {
            ViewpointMsg viewpointMsg =
                    viewpointMsgService.get(form.getViewpoint_id());
            if (viewpointMsg == null) {
                return VOUtil.genErrorResult(HSBCPSErrorCodes.ViewpointErrorInfo.VIEWPOINT_NOT_EXIST,
                        HSBCPSBackVO.class);
            }
            ViewpointRecom viewpointRecom = new ViewpointRecom();
            viewpointRecom.setCompany_id(form.getCompany_id());
            List<ViewpointRecom> viewpointRecomlist = viewpointRecomService.query(viewpointRecom);
            //校验已推荐个数
            if (viewpointRecomlist !=null && viewpointRecomlist.size() >= 3) {
                return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.BEYOND_RECOMMEND_COUNT, HSBCPSBackVO.class);
            }
            viewpointRecom.setViewpointmsg_id(Long.valueOf(form.getViewpoint_id()));
            //校验是否已经推荐
            viewpointRecomlist = viewpointRecomService.query(viewpointRecom);
            if (viewpointRecomlist == null || viewpointRecomlist.size() == 0) {
                viewpointRecomService.save(viewpointRecom);
            }
            return VOUtil.genSuccessBackVo();
        } else if ("2".equals(form.getRecommend_flag())) {
            ViewpointRecom recom = new ViewpointRecom();
            recom.setCompany_id(form.getCompany_id());
            recom.setViewpointmsg_id(Long.valueOf(form.getViewpoint_id()));
            viewpointRecomService.deleteByViewpoint(recom);
            return VOUtil.genSuccessBackVo();
        } else {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.RECOMMEND_FLAG_NOT_EXIST
                    , HSBCPSBackVO.class);
        }
    }

    public HSBCPSBackVO publicManageCombineRecommend(PublicManageCombineRecommendForm form) {
        if ("1".equals(form.getRecommend_flag())) {
            String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
            //校验推荐个数
            RecommendedCombForm recommendedCombForm = new RecommendedCombForm();
            recommendedCombForm.setSendercomp_id(form.getCompany_id());
            recommendedCombForm.setUser_token(user_token);
            RecommendedCombQryBackVo recommendInterestQryBackVo = ledT2Service.func_comb_recommend(recommendedCombForm);
            if (!VOUtil.isSuccess(recommendInterestQryBackVo)) {
                UserTokenUtil.deleteUserToken(user_token);
                return VOUtil.genBGErrorResult(recommendInterestQryBackVo.getError_no(), recommendInterestQryBackVo.getError_info(), InterestRankQryBackVo.class);
            }
            if (recommendInterestQryBackVo.getData_list()!=null && recommendInterestQryBackVo.getData_list().size()>=3) {
                UserTokenUtil.deleteUserToken(user_token);
                return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.BEYOND_RECOMMEND_COUNT, HSBCPSBackVO.class);
            }
            ExtcombineCombineRecommendForm form1 = new ExtcombineCombineRecommendForm();
            form1.setCompany_id(form.getCompany_id());
            form1.setUser_token(user_token);
            form1.setCombi_code(form.getCombi_code());
            ExtcombineCombineRecommendVo back = ledSecT2Service.func_extcombine_combine_recommend(form1);
            UserTokenUtil.deleteUserToken(user_token);
            if (!VOUtil.isSuccess(back)) {
                return VOUtil.genBGErrorResult(back.getError_no(), back.getError_info(), HSBCPSBackVO.class);
            }
        } else if ("2".equals(form.getRecommend_flag())) {
            String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
            ExtcombineCombineRecommendCancelForm form1 = new ExtcombineCombineRecommendCancelForm();
            form1.setCompany_id(form.getCompany_id());
            form1.setUser_token(user_token);
            form1.setCombi_code(form.getCombi_code());
            ExtcombineCombineRecommendCancelVo back = ledSecT2Service.func_extcombine_combine_recommend_cancel(form1);
            UserTokenUtil.deleteUserToken(user_token);
            if (!VOUtil.isSuccess(back)) {
                return VOUtil.genBGErrorResult(back.getError_no(), back.getError_info(), HSBCPSBackVO.class);
            }
        } else {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.RECOMMEND_FLAG_NOT_EXIST
                    , HSBCPSBackVO.class);
        }
        return VOUtil.genSuccessBackVo();
    }

    public PmAdviserRecommendList publicManageAdviserRecommendQry(UserVO form) {
        PmAdviserRecommendList result = new PmAdviserRecommendList();
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
        RecommendInterestForm recommendInterestForm = new RecommendInterestForm();
        recommendInterestForm.setUser_token(user_token);
        RecommendInterestQryBackVo backVo = ledT2Service.func_recommend_interest_query(recommendInterestForm);
        UserTokenUtil.deleteUserToken(user_token);
        if (!VOUtil.isSuccess(backVo)||backVo.getData_list()==null) {
            return VOUtil.genBGErrorResult(backVo.getError_no(), backVo.getError_info(), PmAdviserRecommendList.class);
        }
        List<RecommendInterestResult> list = backVo.getData_list();
        List<PMAdviserRecomendListData> dataList = new ArrayList<>();
        for (RecommendInterestResult recommendInterestResult : list) {
            PMAdviserRecomendListData pmAdviserRecomendListData = new PMAdviserRecomendListData();
            BeanUtils.copyProperties(recommendInterestResult, pmAdviserRecomendListData);
            pmAdviserRecomendListData.setSendercomp_id(form.getCompany_id());
            pmAdviserRecomendListData.setCompany_id(form.getCompany_id());
            dataList.add(pmAdviserRecomendListData);
        }
        result.setData_list(dataList);
        return VOUtil.setSuccessResult(result);
    }

    public InterestRankQryBackVo publicManageAdviserQry(PublicManageAdviserQryForm form) {
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
        InterestRankForm form1 = new InterestRankForm();
        form1.setUser_token(user_token);
        form1.setRequest_num(form.getRequest_num());
        form1.setPosition_str(form.getPosition_str());
        form1.setAlias_name(form.getAlias_name());
        form1.setMobile_tel(form.getMobile_tel());
        form1.setComorder_type("4");
        InterestRankQryBackVo back = ledT2Service.func_interest_rank_query(form1);
        UserTokenUtil.deleteUserToken(user_token);
        if (!VOUtil.isSuccess(back)) {
            return VOUtil.genBGErrorResult(back.getError_no(), back.getError_info(), InterestRankQryBackVo.class);
        }
        return VOUtil.setSuccessResult(back);

    }

    public PMCombineRecommendList publicManageCombineRecommendQry(UserVO userVo) {
        PMCombineRecommendList pmCombineRecommendList = new PMCombineRecommendList();
        String user_token = UserTokenUtil.addPublicManageUserToken(userVo.getCompany_id(), userVo.getCompany_id());
        RecommendedCombForm form = new RecommendedCombForm();
        form.setCompany_id(userVo.getCompany_id());
        form.setUser_token(user_token);
        RecommendedCombQryBackVo recommendInterestQryBackVo = ledT2Service.func_comb_recommend(form);
        UserTokenUtil.deleteUserToken(user_token);
        if (!VOUtil.isSuccess(recommendInterestQryBackVo)||recommendInterestQryBackVo.getData_list()==null) {
            return VOUtil.genBGErrorResult(recommendInterestQryBackVo.getError_no(), recommendInterestQryBackVo.getError_info(),
                    PMCombineRecommendList.class);
        }
        List<RecommendedCombResult> bg_list = recommendInterestQryBackVo.getData_list();
        List<PMCombineRecommendListData> resultList = new ArrayList<>();
        for (RecommendedCombResult recommendedCombResult : bg_list) {
            PMCombineRecommendListData data = new PMCombineRecommendListData();
            BeanUtils.copyProperties(recommendedCombResult, data);
            resultList.add(data);
        }
        pmCombineRecommendList.setData_list(resultList);
        return VOUtil.setSuccessResult(pmCombineRecommendList);
    }

    public PMViewpointRecommendList publicManageViewpointRecommendQry(PMViewpointRecommendQryForm form) {
        PMViewpointRecommendList pmCombineRecommendList = new PMViewpointRecommendList();
        ViewpointRecomQueryForm viewpointRecomQueryForm = new ViewpointRecomQueryForm();
        viewpointRecomQueryForm.setClose_status(CloseStatus.NORMAL.getVal());
        viewpointRecomQueryForm.setCompany_id(form.getCompany_id());
        viewpointRecomQueryForm.setRequest_num(form.getRequest_num());
        viewpointRecomQueryForm.setPage_no(form.getPage_no());
        List<ViewpointMsg> viewpointMsgs = viewpointRecomService.queryViewpointRecomList(viewpointRecomQueryForm);
        List<PMViewpointRecommendListData> dataList = new ArrayList<>();
        for (ViewpointMsg viewpointMsg : viewpointMsgs) {
            PMViewpointRecommendListData data = new PMViewpointRecommendListData();
            BeanUtils.copyProperties(viewpointMsg, data);
            UserEntity userEntity = new UserEntity(viewpointMsg.getUser_id());
            userEntity = userService.getUserEntity(userEntity);
            data.setAlias_name(userEntity.getAlias_name());
            data.setMobile(userEntity.getMobile());
            dataList.add(data);
        }
        pmCombineRecommendList.setData_list(dataList);
        return VOUtil.setSuccessResult(pmCombineRecommendList);
    }

    public CombRankQryBackVo publicManageCombineQry(PublicManageCombineQryForm form) {
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
        CombRankForm form1 = new CombRankForm();
        form1.setUser_token(user_token);
        form1.setRequest_num(form.getRequest_num());
        form1.setPosition_str(form.getPosition_str());
        form1.setCombi_name(form.getCombi_name());
        form1.setCombi_code(form.getCombi_code());
        form1.setComorder_type("8");
        CombRankQryBackVo back = ledT2Service.func_comb_rank(form1);
        if (!VOUtil.isSuccess(back)) {
            return VOUtil.genBGErrorResult(back.getError_no(), back.getError_info(), CombRankQryBackVo.class);
        }
        UserTokenUtil.deleteUserToken(user_token);
        return VOUtil.setSuccessResult(back);
    }

    public PMViewpointList publicManageViewpointQry(ViewpointPublicManageQryForm form) {

        PMViewpointList pmViewpointList = new PMViewpointList();
        ViewpointQueryForm viewpointQueryForm = new ViewpointQueryForm();
        viewpointQueryForm.setCompany_id(form.getCompany_id());
        viewpointQueryForm.setRequest_num(form.getRequest_num());
        viewpointQueryForm.setPage_no(form.getPage_no());
        List<ViewpointUserView> viewpointMsgs = viewpointMsgService.queryByPublicManage(form);
        Integer count = viewpointMsgService.countByPublicManage(form);
        List<PMViewpointListData> dataList = new ArrayList<>();
        for (ViewpointUserView viewpointMsg : viewpointMsgs) {
            PMViewpointListData data = new PMViewpointListData();
            BeanUtils.copyProperties(viewpointMsg, data);
            dataList.add(data);
        }
        pmViewpointList.setData_list(dataList);
        pmViewpointList.setTotal_item_num(count);
        return VOUtil.setSuccessResult(pmViewpointList);

    }
    public PMUserSubscribeCombineList userSubscibeCombineListQry(PMUserSubscribeCombineQryForm form) {
        PMUserSubscribeCombineList result = new PMUserSubscribeCombineList();
        SubscriptionCombineForm subscriptionCombineForm = new SubscriptionCombineForm();
        subscriptionCombineForm.setRequest_num(form.getRequest_num());
        subscriptionCombineForm.setPosition_str(form.getPosition_str());
        subscriptionCombineForm.setUser_id(form.getUser_id() == null ? null : String.valueOf(form.getUser_id()));
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(), form.getCompany_id());
        subscriptionCombineForm.setUser_token(user_token);
        SubscriptionCombineQryBackVo subscriptionCombineQryBackVo = ledT2Service.func_comb_subscription(subscriptionCombineForm);
        UserTokenUtil.deleteUserToken(user_token);
        List<PMUserSubscribeCombineListData> listDataList = new ArrayList<>();
        if (subscriptionCombineQryBackVo.getData_list() != null) {
            for (SubscriptionCombineResult subscriptionCombineResult : subscriptionCombineQryBackVo.getData_list()) {
                PMUserSubscribeCombineListData pmUserSubscribeCombineListData = new PMUserSubscribeCombineListData();
                BeanUtils.copyProperties(subscriptionCombineResult, pmUserSubscribeCombineListData);
                listDataList.add(pmUserSubscribeCombineListData);
            }
        }
        result.setData_list(listDataList);
        return VOUtil.setSuccessResult(result);
    }


    @Override
    public CombineApplyVerifyVo func_combine_apply_verify(CombineApplyVerifyForm param) {
        String user_token = UserTokenUtil.addPublicManageUserToken(param.getSendercomp_id(),param.getSendercomp_id());
        param.setUser_token(user_token);
        CombineApplyVerifyVo combineApplyVerifyVo=ledThreeT2Service.func_combine_apply_verify(param);
        if (!VOUtil.isSuccess(combineApplyVerifyVo)) {
            return VOUtil.genBGErrorResult(combineApplyVerifyVo.getError_no(), combineApplyVerifyVo.getError_info(), CombineApplyVerifyVo.class);
        }
        if(null==combineApplyVerifyVo){
            return VOUtil.setSuccessResult(new CombineApplyVerifyVo());
        }
        CombinationEntity combinationEntity = combineService.getCombinationByCode(combineApplyVerifyVo.getSerial_no().toString());//根据流水查询组合
        UserTokenUtil.deleteUserToken(user_token);
        if(null==combinationEntity){
            return VOUtil.setSuccessResult(new  CombineApplyVerifyVo());
        }
        Integer closeStatus=combinationEntity.getClose_status();
        if("0".equals(closeStatus)){
            combinationEntity.setCombination_code(combineApplyVerifyVo.getCombi_code());
            combinationEntity.setClose_status(1);
            combineService.upCombination(combinationEntity);
        }

        return VOUtil.setSuccessResult(combineApplyVerifyVo);
//        return null;
    }

    @Override
    public CombineApplyNotverifyVo func_combine_apply_notverify(CombineApplyNotverifyForm param) {
        String user_token = UserTokenUtil.addPublicManageUserToken(param.getSendercomp_id(), param.getSendercomp_id());
        param.setUser_token(user_token);
        CombineApplyNotverifyVo combineApplyNotverifyVo=ledThreeT2Service.func_combine_apply_notverify(param);
        if (!VOUtil.isSuccess(combineApplyNotverifyVo)) {
            return VOUtil.genBGErrorResult(combineApplyNotverifyVo.getError_no(), combineApplyNotverifyVo.getError_info(), CombineApplyNotverifyVo.class);
        }
        if(null==combineApplyNotverifyVo){
            return VOUtil.setSuccessResult(new CombineApplyNotverifyVo());
        }
        UserTokenUtil.deleteUserToken(user_token);
        return VOUtil.setSuccessResult(combineApplyNotverifyVo);
//        return null;
    }

    @Override
    public CombineApplyQryBackVo func_combine_apply_qry(CombineApplyQryForm param) {
        String companyId=param.getCompany_id();
        param.setSendercomp_id(companyId);
        String user_token = UserTokenUtil.addPublicManageUserToken(param.getCompany_id(), param.getCompany_id());
        param.setUser_token(user_token);
        CombineApplyQryBackVo combineApplyQryBackVo=ledThreeT2Service.func_combine_apply_qry(param);
        if (!VOUtil.isSuccess(combineApplyQryBackVo)) {
            return VOUtil.genBGErrorResult(combineApplyQryBackVo.getError_no(), combineApplyQryBackVo.getError_info(), CombineApplyQryBackVo.class);
        }
        if(null==combineApplyQryBackVo){
            return VOUtil.setSuccessResult(new CombineApplyQryBackVo());
        }
        UserTokenUtil.deleteUserToken(user_token);
        return VOUtil.setSuccessResult(combineApplyQryBackVo);
    }

    /**
     * 投顾创建
     * 1.中台leader注册
     * 2.中台user注册
     * 3.调用后台用户注册
     * 4.调用投顾认证接口
     **/
    @Override
    @Transactional
    public LeaderRegisterBack leaderRegister(UserCenterLeaderRegisterForm form)  {
        LeaderRegisterBack leaderRegisterBack = new LeaderRegisterBack();
        if(StringUtils.isNotBlank(form.getInvite_code())) {
            //验证码是否被使用
            UserEntity inviteUser = userService.findByInviteCode(new UserEntity(form.getInvite_code()));
            if (null != inviteUser) {
                return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.INVITE_CODE_IS_USED, LeaderRegisterBack.class);
            }
        }
        //投顾没有昵称 只落地真实姓名
        form.setAlias_name(form.getClient_name());
        UserEntity qryUser = new UserEntity();
        qryUser.setUser_type(String.valueOf(UserType.LEADER.getVal()));
        qryUser.setMobile(form.getMobile_tel());
        UserEntity verifyUser = userService.getUserEntity(qryUser);
        //校验投顾是否已注册
        if (null != verifyUser){
            leaderRegisterBack.setUser_id(verifyUser.getUser_id());
            return VOUtil.setSuccessResult(leaderRegisterBack);
        }
        com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.ifs.RegisterForm userRegisterForm =
                new com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.ifs.RegisterForm();
        userRegisterForm.setLogin_account(form.getMobile_tel());
        userRegisterForm.setAuth_password(MD5Util.encrypt(form.getPassword()));
        userRegisterForm.setUser_type("2");
        userRegisterForm.setTenant_id(form.getCompany_id());
        userRegisterForm.setAlias_name(form.getAlias_name());
        userRegisterForm.setRegister_flag("2");
        userRegisterForm.setMobile(form.getMobile_tel());
        userRegisterForm.setBranch_no(form.getBranch_no()+"");
        Leader leader = new Leader(form.getAlias_name(), form.getMobile_tel(),
                System.currentTimeMillis(), form.getCompany_id());
        leader.setJob_state(LeaderJobStateType.ON.getVal());
        getLeaderInfoByForm(leader, form);
        leaderService.addLeader(leader);
        //注册到ITN
        AuthuserRegbindForm authuserRegbindForm = new AuthuserRegbindForm();
        authuserRegbindForm.setBusinsys_no(Long.valueOf(1001));
        authuserRegbindForm.setSendercomp_id(form.getSendercomp_id());
        authuserRegbindForm.setMobile_tel(form.getMobile_tel());
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(),form.getSendercomp_id());
        authuserRegbindForm.setUser_token(user_token);
        AuthuserRegbindVo authuserRegbindVo = new AuthuserRegbindVo();
        try {
            authuserRegbindVo = lightinvestingService.func_services_authuser_regbind(authuserRegbindForm);
            UserTokenUtil.deleteUserToken(user_token);
        }catch (Exception e){
            //成功与否不要影响注册流程
            e.printStackTrace();
        }
        if (authuserRegbindVo == null || authuserRegbindVo.getUser_id() == null){
            authuserRegbindVo.setUser_id("0");
        }
        long leader_id = leader.getLeader_id();
        UserEntity userEntity = new UserEntity();
        userEntity.setUser_id(form.getUser_id());
        userEntity.setSendercomp_id(form.getSendercomp_id());
        userEntity.setCompany_id(form.getCompany_id());
        userEntity.setAccounts_id(leader_id);
        userEntity.setPassword(MD5Util.encrypt(form.getPassword()));
        userEntity.setUser_name(form.getMobile_tel());
        userEntity.setUser_type(String.valueOf(UserType.LEADER.getVal()));
        userEntity.setUser_status(String.valueOf(UserStatus.WAITAUTH.getVal()));
        userEntity.setAlias_name(form.getAlias_name());
        userEntity.setMobile(form.getMobile_tel());
        userEntity.setInvite_code(form.getInvite_code());
        userService.addUser(userEntity);
        leaderRegisterBack.setUser_id(userEntity.getUser_id());
        return BgLeaderRegister(form, leaderRegisterBack.getUser_id());
    }

    /**
     * 后台注册
     * */
    private LeaderRegisterBack BgLeaderRegister(UserCenterLeaderRegisterForm form, long user_id){
        LeaderRegisterBack leaderRegisterBack = new LeaderRegisterBack();
        leaderRegisterBack.setUser_id(user_id);
        UserEntity userEntity = userService.getUserEntity(new UserEntity(user_id));
        RegForm regForm = HttpToBGFormUtil.registFormSwap(form);
        regForm.setUser_id(String.valueOf(user_id));
        String user_token = UserTokenUtil.addPublicManageUserToken(form.getCompany_id(),form.getCompany_id());
        regForm.setUser_token(user_token);
        RegBackVo regBackVo =  lightinvestingService.func_register_authuser(regForm);
        UserTokenUtil.deleteUserToken(user_token);
        if (regBackVo.getSerial_no() == null){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return VOUtil.genBGErrorResult(regBackVo.getError_no(),regBackVo.getError_info(), LeaderRegisterBack.class);
        }
        return VOUtil.setSuccessResult(leaderRegisterBack);
    }

    /**
     * 投顾信息完善
     * */
    private void getLeaderInfoByForm(Leader leader, UserCenterLeaderRegisterForm form) {
        leader.setJob_title(form.getJob_title());
        leader.setProfile(form.getBackground_info());
        leader.setSex(form.getSex());
        leader.setQq(form.getQq());
        leader.setProfile(form.getBackground_info());
        leader.setId_no(form.getId_no());
        leader.setCertificate_id(form.getQualify_no());
    }
}